package cn.ac.cintcm.prespring.cluster.name;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.solr.core.Config;


public class WordFrequency {
	private static String fjUrl = Config.getInstanceDir()+ "data/tempFiles/PRES_FJ_Name_Word.txt";
	
	private static String fjNameWordFreq = Config.getInstanceDir()+ "data/tempFiles/PRES_FJ_Name_Word_Freq_Src.txt";

	public static void main(String[] args) throws Exception{
		calcWordFreqWithSource(readWordFromText());
	}
	
	/**
	 * @return list(FjName)
	 */
	public static List<String> readFromText() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(fjUrl), "UTF8"));
		String line = "";
		List<String> words = new ArrayList<String>();
		while ((line = reader.readLine()) != null) {
			String[] s = line.split(":");
			if (s.length == 2) {
				words.add(s[0]);
			}
		}
		System.out.println("words:"+words.size());
		reader.close();
		
		return words;
	}
	
	
	public static List<Word> readWordFromText() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(fjUrl), "UTF8"));
		String line = "";
		List<Word> words = new ArrayList<Word>();
		while ((line = reader.readLine()) != null) {
			String[] s = line.split(":");
			if (s.length == 2) {
				Word word=new Word();
				word.setText(s[0]);
				word.setSource(s[1]);
				word.addSource(s[1]);
				words.add(word);
			}
		}
		System.out.println("words:"+words.size());
		reader.close();
		
		return words;
	}
	
	public static void calcWordFreq(List<String> words) throws Exception{
		Map<String,Integer> map=new HashMap<String,Integer>();
		for(int i=0;i<words.size();i++){
			String word=words.get(i);
			if(map.get(word)!=null){
				int freq=map.get(word);
				freq++;
				map.put(word,freq);
			}else{
				map.put(word,1);
			}
		}
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(fjNameWordFreq, false), "GB2312"));
		StringBuilder sb = new StringBuilder();
		Set<String> keys=map.keySet();
		Iterator<String> iter=keys.iterator();
		while(iter.hasNext()){
			String word=iter.next();
			sb.append(word + ","+map.get(word));				
			writer.append(sb.toString());
			writer.write("\r\n");
			sb.setLength(0);
		}
		writer.close();
		map.clear();
		map = null;
	}
	

	public static void calcWordFreqWithSource(List<Word> words) throws Exception{
		Map<String,Word> map=new HashMap<String,Word>();
		for(int i=0;i<words.size();i++){
			Word word=words.get(i);
			if(map.get(word.getText())!=null){
				Word w=map.get(word.getText());
				w.setFrequency(w.getFrequency()+1);
				w.addSource(word.getSource());
				map.put(word.getText(),w);
			}else{
				map.put(word.getText(),word);
			}
		}
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(fjNameWordFreq, false), "GB2312"));
		StringBuilder sb = new StringBuilder();
		Set<String> keys=map.keySet();
		Iterator<String> iter=keys.iterator();
		while(iter.hasNext()){
			String text=iter.next();
			Word word=map.get(text);
			if(!word.getText().equals(text)){
				System.err.println("error!not equal");
			}
			if(!word.getSources().contains(word.getSource())){
				System.err.println("error!not contain");
			}
			Set<String> s=word.getSources();
			Iterator<String> iter2=s.iterator();
			String srcs="";
			while(iter2.hasNext()){
				srcs+=iter2.next()+";";
			}
			if(srcs.length()>1){
				srcs=srcs.substring(0, srcs.length()-1);
			}
			sb.append(text + "    "+word.getFrequency()+"    "+srcs);				
			writer.append(sb.toString());
			writer.write("\r\n");
			sb.setLength(0);
		}
		writer.close();
		map.clear();
		map = null;
	}
}
